package com.spzx.common.rabbit.service;

import com.alibaba.fastjson2.JSON;
import com.spzx.common.core.utils.uuid.UUID;
import com.spzx.common.rabbit.entity.GuiguCorrelationData;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 发送消息
     *
     * @param exchange   交换机
     * @param routingKey 路由键
     * @param message    消息
     */
//    public boolean sendMessage(String exchange, String routingKey, Object message) {
//        rabbitTemplate.convertAndSend(exchange, routingKey, message);
//        return true;
//    }


    /**
     * 改进：发送消息
     *  发消息时携带关联数据
     * @param exchange
     * @param routingKey
     * @param message
     * @return
     */
    public boolean sendMessage(String exchange, String routingKey, Object message) {
        GuiguCorrelationData guiguCorrelationData = new GuiguCorrelationData();
        String uuid = "mq:"+UUID.randomUUID().toString().replaceAll("-","");
        guiguCorrelationData.setId(uuid);
        guiguCorrelationData.setMessage(message);
        guiguCorrelationData.setExchange(exchange);
        guiguCorrelationData.setRoutingKey(routingKey);


        //发送消息，携带关联数据
        rabbitTemplate.convertAndSend(exchange, routingKey, message,guiguCorrelationData);

        //把关联数据存储到redis中，便于RabbitTemplate.ReturnsCallback回调函数通过请求头获取关联数据id，然后从redis中获取关联数据，进行消息重发
        //存储时间，要超过多次重试(重试有可能间隔一定时间进行重试)跨越时间。
        redisTemplate.opsForValue().set(uuid, JSON.toJSONString(guiguCorrelationData),10, TimeUnit.MINUTES);
        return true;
    }

    /**
     * 发送延迟消息
     * @param exchange
     * @param routingKey
     * @param message
     * @param delayTime
     */
    public Boolean sendDelayMessage(String exchange, String routingKey, Object message, int delayTime) {
        GuiguCorrelationData guiguCorrelationData = new GuiguCorrelationData();
        String uuid = "mq:"+UUID.randomUUID().toString().replaceAll("-","");
        guiguCorrelationData.setId(uuid);
        guiguCorrelationData.setMessage(message);
        guiguCorrelationData.setExchange(exchange);
        guiguCorrelationData.setRoutingKey(routingKey);
        guiguCorrelationData.setDelay(true); //设置发送延迟消息
        guiguCorrelationData.setDelayTime(delayTime);


        //把关联数据存储到redis中，便于RabbitTemplate.ReturnsCallback回调函数通过请求头获取关联数据id，然后从redis中获取关联数据，进行消息重发
        //存储时间，要超过多次重试(重试有可能间隔一定时间进行重试)跨越时间。
        redisTemplate.opsForValue().set(uuid, JSON.toJSONString(guiguCorrelationData),10, TimeUnit.MINUTES);

        //发送消息，携带关联数据
        rabbitTemplate.convertAndSend(exchange, routingKey, message, (message1) -> {
            message1.getMessageProperties().setDelay(delayTime*1000); //单位毫秒
            return message1;
        }, guiguCorrelationData);

        return true;
    }
}